function gvec = plot_all_autocorr_Summary(acmat, ac_data, in)
% ===================================================
% This function will produce plots of the cross-correlations for each
% series in in.varnames, for all of the models in in.modenames as well
% as the data
%
% INPUTS:
% acmat:        : [nz nz ncor nsim nmode] matrix holding the simulated
%               : cross-correaltions from each mode
% ac_data       : cross-correlations of data
% in.modenames  : names of different models; either the first or last entry
%                 should be "data"
% in.varnames   : series to compare auto
% in.savepath   : folder to save plots
% in.savename   : name of file to save
% ==================================================================
% Matt Olson

%% Check the Inputs and Extract Parameters
cucd=cd; 
modenames = in.modenames;
varnames  = in.varnames; 
savepath  = in.savepath; 
savename  = in.savename; 

% Put the 'data' label at the beginning of modenames
if length(modenames) == 1
    
    if ~strcmp(modenames{1},'data')
        display('Changing label to "data"');
        modenames{1} = 'data';
    end

else
    
    if strcmp(modenames{end},'data')
        modenames = [modenames{end} modenames{1:end-1}];
    end
    
    if ~strcmp(modenames{1},'data')
        error('Either first or last label needs to be "data"')
    end
    
end

% Check for the consistency of lengths of names
[nz, nzchk, ncorr, nsim, nmode] = size(acmat); 

if nz ~= length(varnames); 
    error('number of variables does not match length of variable name cell'); 
elseif nz ~= nzchk
    error('in ACMAT, nr should equal nc'); 
end

if nmode + 1 ~= length(modenames);
    error('number of modes does not match the length of moden name cell'); 
end

if nmode + 1 > 7
    error('only up to 6 modes and data is supported');
end
%% Make the Plots

% Style settings for plots
style    ={'k','go-','bx-','r--','cd--','k^:','gh-'}; 
width    =[2    1       1    3      1.5  2      2];

% Find median of simulated sata
med_acmat = median( acmat, 4);
if ~(size(med_acmat,1)==1 && size(med_acmat,2)==1)
    med_acmat = squeeze(med_acmat);
end

% Combine data and simulated data into one structure
all_acmat = cat(4,ac_data,med_acmat);

% Find the number of figures needed
nFigures = ceil(nz^2/16);
gvec=zeros(nFigures,1);

% Find matrices to determine which plots get mapped to which figure
rLabels=zeros(16, nFigures);
cLabels=zeros(16,nFigures);

tempR = reshape( repmat( (1:nz), [nz 1] ), [nz^2 1]);
tempC = reshape( repmat( (1:nz)', [1 nz] ), [nz^2 1]);

rLabels( 1:nz^2 ) = tempR;
cLabels( 1:nz^2 ) = tempC;

for k = 1:nFigures % Loop over each figure
    
    % The last figure may have less than 16 plots, so we account for this
    if k == nFigures
        
        gvec(k) = figure;
        rows = rLabels(:,k);
        cols = cLabels(:,k);
        
        rows = rows(rLabels(:,k) > 0);
        cols = cols(cLabels(:,k) > 0);
        nRows = length(rows);        
    else
        
        gvec(k) = figure;
        rows = rLabels(:,k);
        cols = cLabels(:,k);
        nRows = length(rows);
    end
                   
    for i = 1:nRows % Loop over all of the pairs of series in the rows and cols
       
       subplot(4,4,i)
       
       for m = 1:nmode+1 % Loop over the different modes
           
           plot( squeeze( all_acmat(rows(i), cols(i), :, m) ),style{m},'LineWidth',width(m))
           set(gca,'box','off');
           hold on       
       end
                     
       % Add title to each subplot
       titStr=[varnames{rows(i)},'$$_t$$','   ',varnames{cols(i)},'$$_{t+s}$$'];
       title(titStr,'interpreter','latex');
       
       axis tight; 
      
       ylabel('$$\rho$$','interpreter','latex');
       hold off
       
    end
    
    % Make each figure look pretty
    hand = legend(modenames,'Orientation','horizontal');
    p = get(hand,'Position');
    p(1) = 0.35;
    p(2) = 0;
    set(hand,'Position',p);
   
    set(gvec(k),'PaperOrientation','Landscape');
    set(gvec(k),'PaperPosition',[0.2 0.1 10.5 8.3]);
end

% Save all of the figs to one pdf
plots2PDF(gvec, savename, savepath);
close all;

end














